//
//  AppealViewController.m
//  Utils
//
//  Created by enghou on 2018/6/22.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "AppealViewController.h"
#import "LocalPersistent.h"
#import "AppealListItemModel.h"
#import "HandleAppealViewController.h"
#import "HandleHistoryViewController.h"
#import "AppealSearchViewController.h"
#import "AppealDetailViewController.h"
@interface AppealViewController ()

@property(nonatomic,strong)NSDictionary *choosenParam;

@end

@implementation AppealViewController


-(NSString *)cellClass{
    return @"AppealListCell";
}

-(NSString *)modelClsName{
    return @"AppealListItemModel";
}

-(NSString *)path{
    return @"/mobile/comprehensiveGovernance/appeal/getAppealListByAppealStateCode.action";
}

-(NSDictionary *)param{
//    0,50-待办列表,1,69-已办列表,2,70-已办结,3,90-历史遗留列表)
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = [NSString stringWithFormat:@"%li",self.page];
    param[@"rows"] = @"10";
    switch (self.type) {
        case 0:
            param[@"appealStateCode"] = @"50";
            break;
        case 1:
            param[@"appealStateCode"] = @"69";
            break;
        case 2:
            param[@"appealStateCode"] = @"70";
        default:
            param[@"appealStateCode"] = @"90";
            break;
    }
    if ([self.choosenParam isKindOfClass:[NSDictionary class]]) {
        [param addEntriesFromDictionary:self.choosenParam];
    }
    return param;
}

-(void)switchType:(NSInteger)type button:(UIButton *)titleButton{
    switch (type) {
        case 0:
        {
            self.path = kProblemPending;
            [self beginRefresh];
            [titleButton setTitle:@"诉求待处理" forState:UIControlStateNormal];
        }
            break;
        case 1:
        {
            self.path = kProblemDone;
            [self beginRefresh];
            [titleButton setTitle:@"诉求已处理" forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            self.path = kProblemDone;
            [self beginRefresh];
            [titleButton setTitle:@"诉求已办结" forState:UIControlStateNormal];
        }
            break;
        case 3:
        {
            self.path = kProblemSubPending;
            [titleButton setTitle:@"历史遗留" forState:UIControlStateNormal];
            [self beginRefresh];
        }
            break;
        default:
            break;
    }
    self.titles = @[@"诉求待处理",@"诉求已处理",@"诉求已办结",@"历史遗留"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)searchAction{
    AppealSearchViewController *search = [[AppealSearchViewController alloc]init];
    search.theDelete = self;
    search.title = @"诉求查询";
    [self push:search];
}

-(NSDictionary *)compareItem{
    NSDictionary *compare = @{@"redSupervise":@"红牌督办",@"ordinarySupervise":@"普通督办",@"yellowSupervise":@"黄牌督办",@"expeditedAppealHandle":@"加急",@"instructionsAppealHandle":@"领导批示"};
    return compare;
}

//-(void)handleProblem:(NSNotification *)not{
//
//}
//
//-(void)processProblem:(NSNotification *)not{
//
//}
//
//-(void)moreProblem:(NSNotification *)not{
//
//}
//
//-(void)preHandleProblem:(NSNotification *)not{
//
//}

-(void)urgentWithItem:(ProblemHandlingItemModel *)model{
    //加急一条
    [self alertSome:@"加急意见" key:@"content" sureAction:^(NSDictionary *param) {
        NSMutableDictionary *p = [NSMutableDictionary dictionaryWithDictionary:param];
        [p setObject:model.ids forKey:@"appealId"];
        [p setObject:model.stepId forKey:@"issueStepId"];
        [p setObject:@"3010" forKey:@"operationCode"];
        [self beginLoading];
        [[NetWorkerManager sharedInstance]commonAlertInterfaceWithPath:@"/mobile/comprehensiveGovernance/appeal/processingAppealByProcessCode.action" parameter:p successBlock:^(BaseModel *model) {
            [self endLoading];
            [self alertSome:model.msg];
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }];
}

-(void)leaderSpeakWithItem:(ProblemHandlingItemModel *)model{
    [self alertSome:@"领导批示" key:@"content" sureAction:^(NSDictionary *param) {
        NSMutableDictionary *p = [NSMutableDictionary dictionaryWithDictionary:param];
        [p setObject:model.stepId forKey:@"stepRecordId"];
        [p setObject:model.ids forKey:@"appealId"];
        [p setObject:@"3030" forKey:@"operationCode"];
        [self beginLoading];
        [[NetWorkerManager sharedInstance]commonAlertInterfaceWithPath:@"/mobile/comprehensiveGovernance/appeal/processingAppealByProcessCode.action" parameter:p successBlock:^(BaseModel *model) {
            [self endLoading];
            [self alertSome:model.msg];
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }];
}

-(void)forceDoneWithItem:(AppealListItemModel *)model code:(NSString *)code{
    NSString *msg;
    NSString *realCode;
//    (1010-普通督办,1030-黄牌督办,1050-红牌督办,3030-批示,3010-加急)
    if ([code isEqualToString:@"81"]) {
        msg = @"普通督办";
        realCode = @"1010";
    }else if ([code isEqualToString:@"83"]){
        msg = @"黄牌督办";
        realCode = @"1030";
    }else if ([code isEqualToString:@"86"]){
        msg = @"红牌督办";
        realCode = @"1050";
    }
    [self alertSome:msg key:@"content" sureAction:^(NSDictionary *param) {
        NSMutableDictionary *p = [NSMutableDictionary dictionaryWithDictionary:param];
        [p setObject:model.stepId forKey:@"stepRecordId"];
        [p setObject:model.ids forKey:@"appealId"];
        [p setObject:realCode forKey:@"operationCode"];
        [self beginLoading];
        [[NetWorkerManager sharedInstance]commonAlertInterfaceWithPath:@"/mobile/comprehensiveGovernance/appeal/processingAppealByProcessCode.action" parameter:p successBlock:^(BaseModel *model) {
            [self endLoading];
            [self alertSome:model.msg];
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }];
}

-(void)handleProblem:(NSNotification *)not{
    ProblemHandlingItemModel *eventId = not.object;
    HandleAppealViewController *container = [[HandleAppealViewController alloc]init];
    container.type = 1;
    container.eventId = eventId.ids;
    container.operations = eventId.operations;
    container.stepId = eventId.stepId;
    [self push:container];
}

-(void)processProblem:(NSNotification *)not{
    ProblemHandlingItemModel *item = not.object;
    HandleHistoryViewController *handle = [[HandleHistoryViewController alloc]init];
    handle.event_id = item.ids;
    handle.type = 1;
    handle.cellClass = @"HandleHistoryCellTableViewCell";
    handle.modelClsName = @"AppealHandleHistoryItemModel";
    handle.path = @"/mobile/comprehensiveGovernance/appeal/getProcessDescriptionByAppealId.action";
    [self push:handle];
}


-(void)deleteAnItemWithModel:(ProblemHandlingItemModel *)model{
    //删除一条
//    [self beginLoading];
//    [[NetWorkerManager sharedInstance]deleteProblemWithPid:model.ids successBlock:^(BaseModel *model) {
//        [self endLoading];
//        if (model.success) {
//            [self beginRefresh];
//        }else{
//            [self alertSome:model.msg];
//        }
//    } failBlock:^(NSInteger code) {
//        [self endLoading];
//        [self alertSome:kNetWorkFail];
//    }];
}

-(void)moreProblem:(NSNotification *)not{

    typedef void(^ForceHandling)(ProblemHandlingItemModel *,NSString *code);
    ForceHandling redCard = ^(ProblemHandlingItemModel *model,NSString *code){
        [self forceDoneWithItem:model code:code];
    };
    typedef void(^HandleProblem)(ProblemHandlingItemModel *);
    HandleProblem urgent = ^(ProblemHandlingItemModel *model){
        [self urgentWithItem:model];
    };
    HandleProblem deleteAct = ^(ProblemHandlingItemModel *model){
        [self deleteAnItemWithModel:model];
    };
    HandleProblem leaderSpeak = ^(ProblemHandlingItemModel *model){
        [self leaderSpeakWithItem:model];
    };
    HandleProblem becomeHistory = ^(ProblemHandlingItemModel *model){
//        [self becomeHistoryWithItem:model];
    };
    HandleProblem modify = ^(ProblemHandlingItemModel *model){
//        [self modifyItem:model];
    };
//    @"redSupervise":@"红牌督办",@"ordinarySupervise":@"普通督办",@"yellowSupervise":@"黄牌督办",@"expeditedAppealHandle":@"加急",@"instructionsAppealHandle":@"领导批示",@"deleteAppealHandle":@"删除",@"updateIssue":@"修改",@"historicalIssue":@"历史遗留"
    NSDictionary *compare = @{@"redSupervise":redCard,@"ordinarySupervise":redCard,@"yellowSupervise":redCard,@"expeditedAppealHandle":urgent,@"instructionsAppealHandle":leaderSpeak};
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    1010-普通督办,1030-黄牌督办,1050-红牌督办,3030-批示,3010-加急
    for (NSDictionary *param in self.permissions) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:param[@"value"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *key = param[@"key"];
            if ([key isEqualToString:@"redSupervise"]) {
                ForceHandling result = compare[key];
                result(not.object,@"86");
            }else if ([key isEqualToString:@"yellowSupervise"]){
                ForceHandling result = compare[key];
                result(not.object,@"83");
            }else if ([key isEqualToString:@"ordinarySupervise"]){
                ForceHandling result = compare[key];
                result(not.object,@"81");
            }else{
                HandleProblem result = compare[key];
                result(not.object);
            }
        }];
        [alert addAction:action];
    }
    UIAlertAction *calcel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:calcel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AppealListItemModel *model = self.source[indexPath.row];
    AppealDetailViewController *detail = [[AppealDetailViewController alloc]init];
    detail.model = model;
    detail.title = @"诉求服务详情";
    [self push:detail];
}

-(void)fetchModel:(id)model{
    if ([model isKindOfClass:[NSDictionary class]]) {
        self.choosenParam = model;
        [self beginRefresh];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
